﻿using System.Management;

namespace MyCpuMonitor.Classes.WMI
{
    public class Processor
    {
        #region global vars

        private readonly ManagementObjectSearcher _mos;
        private readonly ManagementObjectCollection _moc;

        #endregion

        #region Ctor

        public Processor()
        {
            _mos = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
            _moc = _mos.Get();
        }

        #endregion

        #region Functions

        public string AddressWidth(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["AddressWidth"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Architecture(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.Architecture(o["Architecture"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Availability(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.Availability(o["Availability"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Caption(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Caption"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string ConfigManagerErrorCode(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.ConfigManagerErrorCode(o["ConfigManagerErrorCode"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string ConfigManagerUserConfig(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["ConfigManagerUserConfig"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string CpuStatus(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.CpuStatus(o["CpuStatus"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string CreationClassName(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["CreationClassName"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string CurrentClockSpeed(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["CurrentClockSpeed"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string CurrentVoltage(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["CurrentVoltage"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string DataWidth(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["DataWidth"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Description(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Description"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string DeviceId(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["DeviceId"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string ErrorCleared(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["ErrorCleared"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string ErrorDescription(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["ErrorDescription"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string ExtClock(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["ExtClock"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Family(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.Family(o["Family"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string InstallDate(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["InstallDate"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string L2CacheSize(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["L2CacheSize"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string L2CacheSpeed(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["L2CacheSpeed"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string L3CacheSize(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["L3CacheSize"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string L3CacheSpeed(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["L3CacheSpeed"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string LastErrorCode(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["LastErrorCode"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Level(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Level"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string LoadPercentage(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["LoadPercentage"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Manufacturer(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Manufacturer"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string MaxClockSpeed(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["MaxClockSpeed"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Name(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Name"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string NumberOfCores(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["NumberOfCores"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string NumberOfLogicalProcessors(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["NumberOfLogicalProcessors"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string OtherFamilyDescription(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["OtherFamilyDescription"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PnpDeviceId(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PNPDeviceID"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PowerManagementCapabilities(int processor)
        {
            //return "Unable to Determine";

            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        var ss = ProcessorDataLookUp.PowerManagementCapabilities(o["PowerManagementCapabilities"].ToString().Trim());
                        //foreach (var s1 in ss)
                        //{
                        //    
                        //}
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PowerManagementSupported(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PowerManagementSupported"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string ProcessorId(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["ProcessorId"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string ProcessorType(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.ProcessorType(o["ProcessorType"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Revision(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Revision"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Role(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Role"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string SocketDesignation(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["SocketDesignation"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Status(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Status"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string StatusInfo(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.StatusInfo(o["StatusInfo"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Stepping(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Stepping"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string SystemCreationClassName(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["SystemCreationClassName"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string SystemName(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["SystemName"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string UniqueId(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["UniqueId"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string UpgradeMethod(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.UpgradeMethod(o["UpgradeMethod"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Version(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Version"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string VoltageCaps(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = ProcessorDataLookUp.VoltageCaps(o["VoltageCaps"].ToString().Trim());
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        #endregion
    }
}
